import * as constants from './constants'
import api from '../../../api/api'
import { message } from 'antd';
import Utils from '../../../common/utils'
import Immutable from 'immutable';

const changeLogin = (result) => ({
	type: constants.CHANGE_LOGIN,
	value: Immutable.fromJS(result)
})

// const changeAvatar = (result) => ({
//   type: constants.CHANGE_AVATAR,
//   value: Immutable.fromJS(result)
// })

const changeWorkData = (result) =>({
  type: constants.CHANGE_WORK,
  value: Immutable.fromJS(result)
})

const changeOrder = (result) => ({
  type: constants.CHANGE_ORDER,
  value: Immutable.fromJS(result)
})

export const changeMenu = (result) => ({
  type: constants.CHANGE_MENU,
  value: Immutable.fromJS(result)
})


export const saveUserInfo = (params) => (dispatch)=> (
  api.saveUserInfo(params).then(res => {
    if(res.code === 20000) {
      message.success('保存成功')
      Utils.local('userData', res.data)
      dispatch(changeLogin(res.data))
    } else {
      message.error(res.msg);
    }
  })
)

// export const saveAvatar = (params) => (dispatch) => (dispatch(changeAvatar(params))) 

export const savePssword = (params) => (dispatch) =>{
  api.resetPwd({
    oldPwd: params.oldPwd,
    newPwd: params.newPwd
  }).then(res => {
    if(res.code === 20000) {
      message.success('密码重置成功！')
    }
  })
}

export const getWork = (params) => (dispatch) => {
  api.getWork(params).then(res => {
    if(res.code === 20000) {
      debugger
      dispatch(changeWorkData(res.data))
    }
  })
}

export const getOrder = (params) => (dispatch) => {
  api.getOrder(params).then(res => {
    if(res.code === 20000) {
      dispatch(changeOrder(res.data))
    }
  })
}

export const bindPhone = (params) => (dispatch) => {
  api.bindPhone(params).then(res => {
    if(res.code === 20000) {
      message.success('号码绑定成功！')
      Utils.local('userData', res.data)
      dispatch(changeLogin(res.data))
      dispatch(changeMenu({
        menu: 'info',
        title: '修改资料'
      }))
    } 
  })
}

